<template>
  <view class="tabbar">
    <view
      v-for="(item, index) in tabList"
      :key="index"
      :class="[
        index === 999 ? 'tab-item-center' : 'tab-item',
        { active: tabbarStore.current === index }
      ]"
      @click="switchTab(item.pagePath, index)"
    >
      <template v-if="index === 999">
        <view class="center-btn">
          <image :src="item.icon" mode="widthFix" />
        </view>
      </template>
      <template v-else>
        <image :src="tabbarStore.current === index ? item.selectedIcon : item.icon" mode="widthFix" />
        <text>{{ item.text }}</text>
      </template>
    </view>
  </view>
</template>

<script setup>
import { onMounted } from 'vue'
import { useCustomTabbarStore } from '@/store'
const tabbarStore = useCustomTabbarStore()

const tabList = [
  {
    pagePath: '/pages/home/index',
    icon: '/static/images/tabbar/home-i.png',
    selectedIcon: '/static/images/tabbar/home-a.png',
    text: '首页'
  },
  // {
  //   pagePath: '/pages/ask/index',
  //   icon: '/static/images/tabbar/community-i.png',
  //   selectedIcon: '/static/images/tabbar/community-a.png',
  //   text: '社区'
  // },
  // {
  //   pagePath: '/pages/msg/index',
  //   icon: '/static/images/tabbar/message.png',
  //   selectedIcon: '/static/images/tabbar/message.png',
  //   text: '消息'
  // },
  {
    pagePath: '/pages/msg/index',
    icon: '/static/images/tabbar/msg-i.png',
    selectedIcon: '/static/images/tabbar/msg-a.png',
    text: '消息'
  },
  {
    pagePath: '/pages/worker/index',
    icon: '/static/images/tabbar/worker-i.png',
    selectedIcon: '/static/images/tabbar/worker-a.png',
    text: '邻工'
  },
  {
    pagePath: '/pages/user/index',
    icon: '/static/images/tabbar/user-i.png',
    selectedIcon: '/static/images/tabbar/user-a.png',
    text: '我的'
  }
]

const switchTab = (url, index) =>{
  if (tabbarStore.current === index) return
  tabbarStore.setCurrent(index)
  uni.reLaunch({ url })
}

onMounted(() => {
  const pages = getCurrentPages()
  const pagePath = '/' + pages[pages.length - 1].route
  const activeIndex = tabList.findIndex(item => item.pagePath === pagePath)
  if (activeIndex >= 0) {
    tabbarStore.setCurrent(activeIndex)
  }
})
</script>

<style scoped>
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 120upx;
  display: flex;
  background: white;
  border-top: 1px solid #eee;
  z-index: 99;
  justify-content: space-between;
  padding-top: 20upx;
}

.tab-item-center{
	 flex: 1;
	 display: flex;
	 justify-content: center;
	 align-items: center;
}

.tab-item {
  flex: 1;
  text-align: center;
  font-size: 24upx;
  color: #9E9E9E;
}

.tab-item.active {
  color: #D12D26;
}

.tab-item image {
  width: 48upx;
  height: 48upx;
}

.tab-item text {
  display: block;
  margin-top: 4upx;
}

/* 中间凸起按钮 */
.center-btn {
  width: 96upx;
  height: 96upx;
  background: white;
  border-radius: 50%;
  margin-top: -96upx;
  box-shadow: 0 0 8px rgba(0,0,0,0.15);
  display: flex;
  justify-content: center;
  align-items: center;
}

.center-btn image {
  width: 96upx;
  height: 96upx;
}
</style>
